var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var store = require('mixins/store')

var _getLogin = SA.get('USER').login;       //获取login值
var wateritem_id  = '';     //上个页面传过来的id
var wateritem_post = {};
var waterItemInt ;

var bindEvent = function(){
    //此处各种dom 操作
    //点击查看更多 进入日志

    $('#waterItemLog').click(function(){
        router('water/status', {back: waterItemInt.waterOrder.orderId})
    })
    $('.comments a').click(function(){
        // console.log(waterItemInt.userOrder.status);
        router('water/comments', {backid: waterItemInt.waterOrder.orderId, backstatus: waterItemInt.userOrder.status})
    })
}

//订单详情中的水列表
var WaterItem_list = React.createClass({
    render: function(){
        var waterItem_data_list = this.props.data;
        return(
            <li>
                <div className="bL_pic"><img src={waterItem_data_list.url}/></div>
                <div className="bL_com">
                    <em>{waterItem_data_list.cardName}</em>
                    <span className="bL_com_pp">
                        <i>品牌：{waterItem_data_list.skuName}</i>
                        <i>规格：{waterItem_data_list.unit}</i>
                        <i>数量：{waterItem_data_list.count}</i>
                    </span>
                </div>
            </li>
        )
    }
})
//订单详情
var WaterItem = React.createClass({
    mixins: [ItemMixin, store('updataStatus')],
    componentWillMount: function(){
        if(this.props.data){
            this.setState({
                pl_data: this.props.data
            })
        }
    },
    willMount: function(){
        console.log(this.state.pl_data)
        if(this.state.pl_data.userOrder.status == 'COMPLETEEVALUATE'){
            return(
                <div className="f_submit mT01 comments"  id="comments_off">
                    <a href="javascript:;" className="f_btn comments_btn">查看评价</a>
                </div>
            )
        }else if(this.state.pl_data.userOrder.status == 'TOEVALUATE'){
            return(
                <div className="f_submit mT01 comments" id="comments_on">
                    <a href="javascript:;" className="f_btn comments_btn">评价</a>
                </div>
            )
        }
    },
    render:function(){
        var waterItem_plhtml = this.willMount();
        var waterItem_data = this.state.pl_data;
        console.log(waterItem_data);
        var items = [];
        if(waterItem_data){
            waterItem_data.waterCards.map(function(item,i){
                items.push(<WaterItem_list key={i} data={item} />);
            });
            var endTimes =  waterItem_data.waterOrder.reservationPickupEndtime.substr(0,16)
            var endtimedata = endTimes.split(" ");
            var waterItem_time = waterItem_data.waterOrder.reservationPickupStarttime.substr(0,16) + '~' + endtimedata[1];
        }
        var wicreateTime = waterItem_data.orderLog.createTime.substr(0,16)
        return (
            <div className="waterItems flex flex-v pB020">
                <div className="flex-1 f-oTouch2">
                    <div className="defalut_head_orders default_pd016 box-shadow" id="waterItemLog">
                        <em>
                            {  (function(){
                                    var _d = waterItem_data.orderLog.description,
                                        _c = waterItem_data.orderLog.createBy;
                                    return _d.indexOf('领单')>-1 ? _c+''+_d: _d;
                                })()
                            }
                        </em>
                        <div className="dHO_link">
                            <p>
                                {wicreateTime}
                            </p>
                            <a href="javascript:;" className="link_more">查看更多 <i className="iconfont icon-xiangyou"></i></a>
                        </div>
                    </div>
                    {waterItem_plhtml}
                    <ul className="default_form mT01">
                        <li>
                            <label>姓名</label>
                            <p className="default_no_update">{waterItem_data.waterOrder.userName}</p>
                        </li>
                        <li>
                            <label>电话</label>
                            <p className="default_no_update">{waterItem_data.waterOrder.userPhone}</p>
                        </li>
                        <li>
                            <label>地址</label>
                            <p className="default_no_update">{waterItem_data.waterOrder.address}</p>
                        </li>
                        <li>
                            <label>预约时间</label>
                            <p className="default_no_update">{waterItem_time}</p>
                        </li>
                        <li>
                            <label>备注</label>
                            <p className="default_no_update">{waterItem_data.waterOrder.remark}</p>
                        </li>
                    </ul>
                    <div className="default_pd016 mT01">
                        <label>订单号</label>
                        <p className="default_no_update">{waterItem_data.waterOrder.bill}</p>
                    </div>
                    <ul className="buyList haveBuyList mT01">
                        {items}
                    </ul>
                    <div className="f_submit mT014">
                        <a href={"tel:"+waterItem_data.provider.contactPhone} className="f_btn">客服电话</a>
                    </div>

                </div>
            </div>
        )
    }
});
function updataStatu(){
    api.req('/wsOrderdetails', wateritem_post, function(data){
        waterItemInt = data
        SA.setter('updataStatus', {pl_data: data})
        SA.deleter('bkstatus')
    })
}

function start(name){
    return pages.new({
        boot: function(){
        },
        trigger:function(self, data){
            this.libs.changeTitle('订单详情');    //更改当前页面标题
            // _boot(data)
            if(data && data.back){
                wateritem_id = data.back;
            }
            wateritem_post = {
                login: SA.get('USER').login,
                // orderId: '50f581ae2cc54a3b8ef94944e2956f05',     //已评论过的id
                // orderId: 'fb418ff601fd4c5dbecdf397498265ac',     //已评论过的id
                orderId: wateritem_id,
                method: 'get'
            };
            if(SA.get('bkstatus')){     //从下个页面返回的一个状态，来重新渲染页面
                updataStatu()
            }else{
                api.req('/wsOrderdetails', wateritem_post, function(data){
                    self.main(self, data)
                })
            }
        },
        main: function(self,data){
            if(data){
                console.log(data);
               waterItemInt = data
               this.render(
                   <WaterItem itemDefaultMethod={bindEvent} data={data} />,
                   document.getElementById(name)
               )
            }
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
